package com.quxiu.system.controller;

import java.util.List;
import javax.annotation.Resource;

import com.quxiu.common.core.exception.ServiceException;
import com.quxiu.common.core.utils.RegexUtils;
import com.quxiu.common.core.utils.StringUtils;
import com.quxiu.common.security.utils.SecurityUtils;
import com.quxiu.system.domain.MagicEntityMeta;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.quxiu.common.log.annotation.Log;
import com.quxiu.common.log.enums.BusinessType;
import com.quxiu.common.security.annotation.RequiresPermissions;
import com.quxiu.system.domain.MagicEntity;
import com.quxiu.system.service.IMagicEntityService;
import com.quxiu.common.core.web.controller.BaseController;
import com.quxiu.common.core.web.domain.AjaxResult;
import com.quxiu.common.core.web.page.TableDataInfo;

/**
 * 数据实体Controller
 *
 * @author quxiu
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/magic/entity")
public class MagicEntityController extends BaseController {
    @Resource
    private IMagicEntityService magicEntityService;

    /**
     * 查询数据实体列表
     */
    @RequiresPermissions("system:entity:list")
    @GetMapping("/list")
    public TableDataInfo list(MagicEntity magicEntity) {
        startPage();
        List<MagicEntity> list = magicEntityService.selectMagicEntityList(magicEntity);
        return getDataTable(list);
    }

    /**
     * 获取数据实体详细信息
     */
    @RequiresPermissions("system:entity:query")
    @GetMapping(value = "/{entityId}")
    public AjaxResult getInfo(@PathVariable("entityId") Long entityId) {
        return success(magicEntityService.selectMagicEntityByEntityId(entityId));
    }

    /**
     * 新增数据实体
     */
    @RequiresPermissions("system:entity:add")
    @Log(title = "数据实体", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MagicEntity magicEntity) {
        this.verifyEntity(magicEntity);
        Long userId = SecurityUtils.getUserId();
        magicEntity.setCreateBy(userId);
        return toAjax(magicEntityService.insertMagicEntity(magicEntity));
    }

    /**
     * 修改数据实体
     */
    @RequiresPermissions("system:entity:edit")
    @Log(title = "数据实体", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MagicEntity magicEntity) {
        if (magicEntity.getEntityId() == null) {
            throw new ServiceException("实体ID不能为空");
        }
        this.verifyEntity(magicEntity);
        Long userId = SecurityUtils.getUserId();
        magicEntity.setUpdateBy(userId);
        return toAjax(magicEntityService.updateMagicEntity(magicEntity));
    }

    /**
     * 验证字段
     * @param magicEntity
     */
    private void verifyEntity(MagicEntity magicEntity) {
        if (magicEntity == null) {
            throw new ServiceException("信息不能为空");
        }
        if (magicEntity.getEntityId() == null) {
            if (StringUtils.isBlank(magicEntity.getEntityName())) {
                throw new ServiceException("实体名称不能为空");
            }
            if (!RegexUtils.matches(RegexUtils.PATTERN_ALPHANUMERIC_UNDERLINE_NO_UNDERSCORE, magicEntity.getEntityName())) {
                throw new ServiceException("实体名称只能包含字母、数字、下划线");
            }
            if (!RegexUtils.matches(RegexUtils.PATTERN_NOT_DB_KEYWORD, magicEntity.getEntityName())) {
                throw new ServiceException("实体名称不能是数据库关键字，请更换名称");
            }
        } else {
            magicEntity.setEntityName(null);
        }

        if (StringUtils.isBlank(magicEntity.getEntityTitle())) {
            throw new ServiceException("实体标题不能为空");
        }
        if (!RegexUtils.matches(RegexUtils.PATTERN_CHINESE_ALPHANUMERIC, magicEntity.getEntityTitle())) {
            throw new ServiceException("实体标题只能包含中文、字母、数字");
        }
        if (!magicEntityService.checkEntityNameUnique(magicEntity)) {
            throw new ServiceException("实体名称已存在");
        }
        if (!magicEntityService.checkEntityTitleUnique(magicEntity)) {
            throw new ServiceException("实体标题已存在");
        }
    }

    /**
     * 删除数据实体
     */
    @RequiresPermissions("system:entity:remove")
    @Log(title = "数据实体", businessType = BusinessType.DELETE)
	@DeleteMapping("/{entityIds}")
    public AjaxResult remove(@PathVariable Long[] entityIds) {
        return toAjax(magicEntityService.deleteMagicEntityByEntityIds(entityIds));
    }
}
